package com.go.where.manager.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.go.where.manager.common.Constants;
import com.go.where.manager.dao.ActivityDao;
import com.go.where.manager.dao.SmallClassDao;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.page.BaseQuery.OrderType;
import com.go.where.manager.pojo.ActivityInfo;
import com.go.where.manager.pojo.SmallClassInfo;
import com.go.where.manager.pojo.user.UserInfoVO;
import com.go.where.manager.service.SmallClassService;

@Service("smallClassService")
public class SmallClassServiceImpl implements SmallClassService {

	@Resource(name = "smallClassDao")
	private SmallClassDao smallClassDao;
	@Resource
	private ActivityDao activityDao;

	@Override
	public SmallClassInfo queryByPrimartKey(String cid) {
		return smallClassDao.getByKey(cid);
	}

	@Override
	public Pagination<SmallClassInfo> queryPage(SmallClassInfo smallClassInfo) {
		if (StringUtils.isNotEmpty(smallClassInfo.getOrderBy())
				&& smallClassInfo.getOrderType() != null) {
			if (smallClassInfo.getOrderType() == OrderType.asc) {
				smallClassInfo.setPage_orderByAsc(smallClassInfo.getOrderBy());

			} else {
				smallClassInfo.setPage_orderByDesc(smallClassInfo.getOrderBy());
			}
		}
		Integer count = smallClassDao.totalCount(smallClassInfo);
		smallClassInfo.calculate();
		List<SmallClassInfo> list = smallClassDao.queryList(smallClassInfo);
		Pagination<SmallClassInfo> page = smallClassDao.queryPagination(count,
				list, smallClassInfo);
		return page;
	}

	@Override
	public int save(SmallClassInfo smallClassInfo, UserInfoVO currentUser) {
		smallClassInfo.setPublishTime(new Date());
		smallClassInfo.setUserId(currentUser.getUserId());
		smallClassInfo.setIsDelete(Constants.NOT_DELETED);
		return smallClassDao.insert(smallClassInfo);
	}

	@Override
	public int update(SmallClassInfo smallClassInfo, UserInfoVO currentUser) {
		smallClassInfo.setUpdateTime(new Date());
		smallClassInfo.setUserId(currentUser.getUserId());
		ActivityInfo ai = new ActivityInfo();
		ai.setSmallClassId(smallClassInfo.getClassId());
		ai.setIsDelete(Constants.DELETED);
		ai.setUpdateTime(new Date());
		ai.setUserId(currentUser.getUserId());
		activityDao.updateByClassId(ai);
		return smallClassDao.update(smallClassInfo);
	}

	@Override
	public String getSubKey() {
		return smallClassDao.getNextKey();
	}

	@Override
	public List<SmallClassInfo> queryByBigClass(SmallClassInfo bigClassId) {
		return smallClassDao.queryByBigClass(bigClassId);
	}

}
